package cn.chenwx.bbsforum.controller;

import cn.chenwx.bbsforum.component.JavaScriptCheck;
import cn.chenwx.bbsforum.entity.Article;
import cn.chenwx.bbsforum.entity.Comment;
import cn.chenwx.bbsforum.entity.Tag;
import cn.chenwx.bbsforum.entity.User;
import cn.chenwx.bbsforum.service.ArticleService;
import cn.chenwx.bbsforum.service.CommentService;
import cn.chenwx.bbsforum.service.TagService;
import cn.chenwx.bbsforum.service.UserService;
import cn.chenwx.bbsforum.utils.BlogJSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class HomeController {
    @Autowired
     private   ArticleService articleService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;


    @GetMapping("/article/index/{pageNum}")
    public String goHome(Model model, @PathVariable("pageNum") Integer pageNum,
                         @RequestParam(value = "tag",defaultValue = "")String tag){
        List<Article> articles = articleService.findAllArticles();
        PageInfo<Article> pageInfo = new PageInfo<>(articles);

        model.addAttribute("articleNum",articleService.getArticleNum());
        model.addAttribute("articles",articles);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("tag",tag);
        model.addAttribute("tags",tagService.findTagsCloud());
        model.addAttribute("tagNum",tagService.countTagsNum());
        model.addAttribute("commentNum",commentService.getAllComments());
        return "home";
    }


    @GetMapping("/article/details/{id}")
    public String test(HttpServletRequest request , @PathVariable("id") int articleId){
        Article article  = articleService.findArticleById(articleId);
        request.setAttribute("info",article);
        User user = userService.findUserById(article.getUserId());
        request.setAttribute("author",user.getUsername());
        List<Comment> comments = commentService.findContentByArticleId(articleId);
        request.setAttribute("comments",comments);
        int commentNum = commentService.findCommentsByArticleId(articleId);
        request.setAttribute("commentNum",commentNum);
        List<Tag> tagsCloud = tagService.findTagsCloud();
        request.setAttribute("tags",tagsCloud);


        return "detail";
    }



    @PostMapping("/article/publishComment1")
    @ResponseBody
    public BlogJSONResult publishComment(@RequestParam("commentContent") String content, Comment comment, HttpSession session, @RequestParam("articleId") String articleId){
        User user = (User) session.getAttribute("user");
        BlogJSONResult blogJSONResult = new BlogJSONResult();
        if (user==null){
            blogJSONResult.setStatus(502);
            return blogJSONResult;
        }else {
            comment.setContent(content);
            comment.setArticleId(Integer.parseInt(articleId));
            comment.setAnswererId(user.getId());

            return BlogJSONResult.ok(commentService.insertComment(comment));
        }
    }




}
